package cn.com.bsoft.kotlinEx

import org.apache.commons.codec.binary.Base64
import org.apache.commons.codec.digest.DigestUtils
import java.io.ByteArrayOutputStream

fun ByteArray.sha265(vararg other:ByteArray?):String{
    var out = ByteArrayOutputStream()
    out.write(this)
    other?.forEach {
        if (it != null)
            out.write(it)
    }
    out.flush()
    val digest = DigestUtils.sha256Hex(out.toByteArray())
    out.close()
    return digest
}

fun ByteArray.md5(vararg other:ByteArray?):String{
    var out = ByteArrayOutputStream()
    out.write(this)
    other?.forEach {
        if (it != null)
            out.write(it)
    }
    out.flush()
    val digest = DigestUtils.md5Hex(out.toByteArray())
    out.close()
    return digest
}

fun ByteArray.base64():String{
    return Base64.encodeBase64String(this)
}

fun String.decodeBase64():ByteArray{
    return Base64.decodeBase64(this)
}